package com.woniuxy.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dto.DataGridView;
import com.woniuxy.dto.MyDto;
import com.woniuxy.pojo.McarParking;

import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import com.woniuxy.vo.McarParkingVo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Controller
public class CarController {
    @Autowired
    private UserService userService;

    @PostMapping("/addcar.do")
    @ResponseBody
    public MyDto addcar(McarParking mcarParking){
        MyDto myDto=new MyDto();
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        mcarParking.setPid(user.getId());
        mcarParking.setpState(0);
        try {

            userService.addcar(mcarParking);
            myDto.setCode(200);
            myDto.setMsg("发布成功");
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);
            myDto.setMsg("发布失败");
        }

        return myDto;
    }

    /**
     * 通过商家id查询商家一共有可用车位
     * @param
     * @return
     */
    @GetMapping("/carNums")
    @ResponseBody
    public Integer carNums(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        return userService.selectCarNums(user.getId());
    }

    /**
     * 通过商家id查询商家一共有多少个已租车位
     * @param
     * @return
     */
    @GetMapping("/carNumson")
    @ResponseBody
    public Integer carNumson( ){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        return userService.selectCarNumson(user.getId());
    }

    /**
     * 被商家封禁的车位个数
     * @param
     * @return
     */
    @GetMapping("/carNumsdo")
    @ResponseBody
    public Integer carNumsdo( ){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        return userService.selectCarNumsdo(user.getId());
    }

    /**
     * 查询车库详细信息,包括地址 和状态模糊查询
     * @param mcarParkingvo
     * @return
     */
    @GetMapping("/cars.do")
    @ResponseBody
    public DataGridView cars(McarParkingVo mcarParkingvo){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        mcarParkingvo.setPid(user.getId());

        return userService.findAllcarsBypid(mcarParkingvo);
    }

    /**
     * 用户删除车位
     * @param id
     * @return
     */
    @DeleteMapping("/carss/{id}")
    @ResponseBody
    public String deletecars(@PathVariable("id") Integer id){
        String x;
        try {
            userService.deleteById(id);
            x="删除成功";
        }catch (Exception e){
            x="删除失败";
        }
        return x;
    }

    /**
     * 用户批量删除车位
     * @param ids
     * @return
     */
    @DeleteMapping("/carsss")
    @ResponseBody
    public String deletecarss(Integer[] ids){
            String x;
        try {

            userService.deleteByIds(ids);
            x="删除成功";
        }catch (Exception e){
            x="删除失败";
        }
        return x;
    }

    /**
     * 系统通过id 修改车位状态
     * @param  id  state
     * @return
     */
    @PutMapping("/setstate")
    @ResponseBody
    public String setstate(@Param("id") Integer id,@Param("state") String state){
        System.out.println("zzzzzz");
        try {
            userService.setParkingState(id,state);
         return "修改成功";
        }catch (Exception e){
            return "修改失败";
        }
    }

    /**
     * 商家通过id 修改车位状态
     * @param id  state
     * @return
     */
    @PutMapping("/setustate")
    @ResponseBody
    public MyDto setustate(@Param("id") Integer id, @Param("state") String uState){
         MyDto myDto=new MyDto();
        try {
            userService.setParkinguState(id,uState);
            myDto.setMsg("修改成功");
            myDto.setCode(200);

        }catch (Exception e){
            myDto.setMsg("修改失败");
            myDto.setCode(-200);
        }
        return myDto;
    }

    /**
     * 商家批量修改车状态
     * @param id
     * @param states
     * @return
     */
    @PutMapping("/setustates")
    @ResponseBody
    public String serustates(@Param("id") Integer id,@Param("states") String[] states){
        try {
            for (String state:states) {

                userService.setParkinguState(id,state);
            }
            return "修改成功";

        }catch (Exception e){
            return "修改失败";
        }
    };
    /**
     * 平台通过id 修改车位状态
     * @param id  state
     * @return
     */
    @PutMapping("/setpstate")
    @ResponseBody
    public String setpstate(Integer id, String pState){

        try {
            userService.setParkingpState(id,pState);
            return "修改成功";
        }catch (Exception e){
            return "修改失败";
        }
    }

    /**
     * 平台批量修改车状态
     * @param id
     * @param states
     * @return
     */
    @PutMapping("/setpstates")
    @ResponseBody
    public String serptates(@Param("id") Integer id,@Param("states") String[] states){
        try {
            for (String state:states) {
                userService.setParkingpState(id,state);
            }
            return "修改成功";

        }catch (Exception e){
            return "修改失败";
        }
    };

    /**
     * 车位详情页面
     * @param mcarParking
     */
    @GetMapping("/toeditCar.do")
    @ResponseBody
    public void editCar(McarParking mcarParking){

    }
}
